package me.leoray.leilog;

import android.util.Log;

import java.util.HashMap;
import java.util.Map;

/**
 * 支持实例的日志类
 * 实例通过ROOT_TAG区分,不同的ROOT_TAG得到不同的实例
 * Created by leoraylei on 16/9/16.
 */

public class Logs {
    private static final String DEFAULT_TAG = "LeiLog";

    /**
     * 键为rootTag名,后面可以随时改变,值为Logs实例
     */
    private static Map<String, Logs> logMap = new HashMap<>();
    private static final Logs DEFAULT_LOGS = new Logs(DEFAULT_TAG);

    private LogPrinter printer = new LogPrinter();

    public Logs(String rootTag) {
        printer.settings().rootTag(rootTag);
    }

    public static Logs get() {
        return get(null);
    }


    /**
     * 通过标签拿到一个Logs对象
     *
     * @param rootTag
     * @return
     */
    public synchronized static Logs get(String rootTag) {
        if (rootTag == null) {
            return DEFAULT_LOGS;
        }
        Logs logs = logMap.get(rootTag);
        if (logs == null) {
            logs = new Logs(rootTag);
            logMap.put(rootTag, logs);
        }
        return logs;
    }

    /**
     * 添加配置,rootTag不同,其他的配置和默认配置共享
     *
     * @param classNamePrefix
     * @param rootTag
     * @return
     */
    public Settings settings(String classNamePrefix, String rootTag) {
        final Settings share = Settings.share(this.defaultSettings());
        share.rootTag(rootTag);
        printer.settings(classNamePrefix, share);
        return share;
    }

    /**
     * 添加配置,处理tag和文件存储策略不同,其他的配置和默认配置共享
     *
     * @param classNamePrefix
     * @param isSaveFile      共享之前的文件存储策略(包括存储级别,存储地址)
     * @param rootTag
     * @return
     */
    public Settings settings(String classNamePrefix, boolean isSaveFile, String rootTag) {
        final Settings share = Settings.share(this.defaultSettings());
        share.rootTag(rootTag);
        if (isSaveFile) {
            share.openLogSave(this.defaultSettings().fileSaveLevel(), this.defaultSettings().logDir());
        } else {
            share.closeLogSave();
        }
        printer.settings(classNamePrefix, share);
        return share;
    }

    /**
     * 添加配置
     *
     * @param classNamePrefix
     * @param settings
     * @return
     */
    public Settings settings(String classNamePrefix, Settings settings) {
        printer.settings(classNamePrefix, settings);
        return settings;
    }

    Settings defaultSettings() {
        return printer.defaultSettings();
    }

    /**
     * 返回当前类的配置
     *
     * @return
     */
    public Settings settings() {
        return printer.settings();
    }


    public Settings rootTag(String tag) {
        return settings().rootTag(tag);
    }

    public void v(String tag, String message, Object... param) {
        printer.v(tag, message, param);
    }

    public void d(String tag, String message, Object... param) {
        printer.d(tag, message, param);
    }

    public void i(String tag, String message, Object... param) {
        printer.i(tag, message, param);
    }

    public void w(String tag, String message, Object... param) {
        printer.w(tag, message, param);
    }

    public void e(String tag, String message, Object... param) {
        printer.e(tag, message, param);
    }

    public void wtf(String tag, String message, Object... param) {
        printer.wtf(tag, message, param);
    }

    public void v(String message, Object... param) {
        printer.v(message, param);
    }

    public void d(String message, Object... param) {
        printer.d(message, param);
    }

    public void i(String message, Object... param) {
        printer.i(message, param);
    }

    public void w(String message, Object... param) {
        printer.w(message, param);
    }

    public void e(String message, Object... param) {
        printer.e(message, param);
    }

    public void wtf(String message, Object... param) {
        printer.wtf(message, param);
    }


    public void v(String tag, String message, Throwable param) {
        printer.v(tag, message, param);
    }

    public void d(String tag, String message, Throwable param) {
        printer.d(tag, message, param);
    }

    public void i(String tag, String message, Throwable param) {
        printer.i(tag, message, param);
    }

    public void w(String tag, String message, Throwable param) {
        printer.w(tag, message, param);
    }

    public void e(String tag, String message, Throwable param) {
        printer.e(tag, message, param);
    }

    public void wtf(String tag, String message, Throwable param) {
        printer.wtf(tag, message, param);
    }

    /* 下面针对XML和JSON字符串的特殊api*/

    public void json(String json) {
        printer.json(json);
    }

    public void json(String prefixLog, String json) {
        printer.json("", Log.DEBUG, prefixLog, json);
    }

    public void json(String tag, int level, String prefixLog, String json) {
        printer.json(tag, level, prefixLog, json);
    }

    public void xml(String xml) {
        printer.xml(xml);
    }

    public void xml(String prefixLog, String xml) {
        printer.json("", Log.DEBUG, prefixLog, xml);
    }

    public void xml(String tag, int level, String prefixLog, String xml) {
        printer.xml(tag, level, prefixLog, xml);
    }

}
